package de.dopichaj.labrador.tools;


import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import de.dopichaj.labrador.index.Index;
import de.dopichaj.labrador.index.IndexVersionException;
import de.dopichaj.labrador.index.TagFactory;
import de.dopichaj.labrador.index.lucene.LuceneFileSystemIndex;
import de.dopichaj.labrador.index.meta.DocumentDigestFactory;
import de.dopichaj.labrador.index.meta.DocumentMetaData;
import de.dopichaj.labrador.index.meta.HitFactory;
import de.dopichaj.labrador.search.hit.Hit;


public final class DigestConverter {

    private static final class Collector implements HitFactory.DigestCollector {

        private final Map<String, Integer> knownXPaths = new HashMap<String, Integer>();
        private int nextXPathID = 0;
        private int docId = 0;
        private int fragmentId = 0;
        public void collectDigest(final DocumentMetaData digest) {
            System.out.println("start transaction;");
            System.out.println("insert into document(id, name) " +
                "values(" + docId + ", '" + digest.getFile() + "');");
            
            final Collection<Hit> hits = digest.getFragments(Arrays.asList(-1)).values();
            
            for (final Hit hit : hits) {
                final String xPath = hit.getXPath();
                final int xPathID;
                if (knownXPaths.containsKey(xPath)) {
                    xPathID = knownXPaths.get(xPath);
                } else {
                    xPathID = nextXPathID;
                    knownXPaths.put(xPath, xPathID);
                    System.out.println("insert into xpath(id, xpath) values (" +
                        xPathID + ", '" + xPath + "');");
                    
                    nextXPathID++;
                }
                System.out.println(
                    "insert into fragment(id, docid, xpathid, length) " +
                    "values(" + fragmentId++ + ", " + docId +
                    ", " + xPathID + ", " +
                    hit.getContentLength() + ");");
            }
            System.out.println("commit;");
            
            docId++;
        }
    }
    
    /**
     * @param args
     * @throws IndexVersionException 
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException, IndexVersionException {
        
        // open the de.dopichaj.labrador.index
        final Index index = new LuceneFileSystemIndex(new File(args[0]));
        final TagFactory tagFactory = index.getTagFactory();
        final HitFactory.DigestCollector collector = new Collector();
        
        HitFactory.readDigests(new DocumentDigestFactory(), tagFactory,
            index.getDigestByteReader(), collector);
    }
}
/*
Copyright (c) 2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/